package com.zoo.diannaopeijian.controller.component;

import com.zoo.diannaopeijian.common.filter.FilterUtil;
import com.zoo.diannaopeijian.controller.BaseController;
import com.zoo.diannaopeijian.domain.Component;
import com.zoo.diannaopeijian.service.ComponentService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 配件控制器
 */
@Controller
@RequestMapping("/component")
public class ComponentController extends BaseController {
    @Resource
    private ComponentService componentService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "component/" + pageName;
    }
//
//    @RequestMapping("/componentManaPage")
//    @LoggerManage(description = "访问标签管理页面")
//    public String componentRoute() {
//        return "component/componentManaPage";
//    }

    @GetMapping("/")
    public ResponseEntity<List<Component>> componentGet() {
        return ResponseEntity.ok(componentService.getComponent(null));
    }

    // filter插件要求的数据
    @GetMapping("/filter")
    public ResponseEntity<List> filterComponentGet() {
        return ResponseEntity.ok(FilterUtil.getFilterData(componentService.getComponent(null), "compId", "compName"));
    }

    @PostMapping("/")
    public ResponseEntity<String> componentPost(Component component) {
        return ResponseEntity.ok(componentService.addComponent(component) ? success : fail);
    }

    @DeleteMapping("/{componentId}")
    public ResponseEntity<String> componentDelete(@PathVariable Integer componentId) {
        return ResponseEntity.ok(componentService.deleteComponentById(componentId) ? success : fail);
    }

    @PutMapping("/")
    public ResponseEntity<String> componentPut(Component component) {
        return ResponseEntity.ok(componentService.updateComponent(component) ? success : fail);
    }
}
